<template>
  <div class="btnDialog">
    <div>
      <!-- v-if="hasAuth('VMS_FULL_FLOW_INVOICE_01')" -->

      <el-button size="small" type="primary"
                 :disabled="$store.state.flowInvoice.selectedItems && !$store.state.flowInvoice.selectedItems.length"
                 
                 @click="openInvoice(true)">开具专票
      </el-button>
      <el-button size="small" type="primary"
                 :disabled="$store.state.flowInvoice.selectedItems && !$store.state.flowInvoice.selectedItems.length"
               
                 @click="openInvoice">开具普票
      </el-button>
      <el-button size="small" @click="lookFlow" type="primary"
                 :disabled="$store.state.flowInvoice.selectedItems && $store.state.flowInvoice.selectedItems.length !== 1">
        查看流水
      </el-button>
      <el-button size="small" @click="splitFn" type="primary"  
                 :disabled="splitMergeDisabled">
        拆分
      </el-button>
      <el-button size="small" @click="mergeFn" type="primary"  
                 :disabled="splitMergeDisabled">
        合并
      </el-button>

      <el-button size="small" @click="reductionFn" type="primary"  
              :disabled="reductionDisabled">
        还原
      </el-button>
    </div>


    <!-- 发票 -->
    <invoicingDialog :invoicingSpectShow="invoicingSpectShow" :invoicingPlainShow="invoicingPlainShow"
                     @invoicingShow="invoicingShow"></invoicingDialog>
    <!-- 查看流水 -->
    <lookFlowDialog v-model="lookFlowShow"></lookFlowDialog>
    <splitDialog v-model="splitShow" @resList='resList'></splitDialog>
    <mergeDialog  ref="merge" v-model="mergeShow" @resList='resList'></mergeDialog>
    <flowMergingSplitDialog ref="flowMergingSplit" @resList="resList"></flowMergingSplitDialog>

  </div>
</template>
<script>
import lookFlowDialog from './lookFlowDialog.vue'
import invoicingDialog from './invoicingDialog.vue'
import splitDialog from './splitDialog.vue'
import mergeDialog from './mergeDialog.vue'
import {mergeListQuery} from "@/api/transactionFlowInvoice";
import addVoiceDialog from "@/views/handInvoiceAdd/addVoiceDialog.vue";
import flowMergingSplitDialog from "@/components/transactionFlowInvoice/flowMergingSplitDialog.vue";
import {checkFlowInvoiceStatus} from "@/api/transactionFlowInvoice";

export default {
  data() {
    return {
      lookFlowShow: false,
      invoicingSpectShow: false,
      invoicingPlainShow: false,
      mergeShow: false,
      splitShow: false,
    }
  },
  computed: {
    //拆分合并按钮是否禁用
    splitMergeDisabled() {
      //如果现在一条流水，并且czlx 不是空的返回true  禁用，
      //如果没有选择一条数据或者多条数据，返回true 禁用

      if(this.$store.state.flowInvoice.selectedItems
          && this.$store.state.flowInvoice.selectedItems.length ===1){
            let r1 =  this.$store.state.flowInvoice.selectedItems[0].czlx !== ''
            let r2 = this.$store.state.flowInvoice.selectedItems[0].czlx !== null

        return r1 && r2 ; // 1和2都是不可以操作
      }
      return true;

    },
    reductionDisabled(){
      if(this.$store.state.flowInvoice.selectedItems
          && this.$store.state.flowInvoice.selectedItems.length ===1){
        //如果现在一条流水，并且czlx 不是空的返回true  禁用，
        return this.$store.state.flowInvoice.selectedItems[0].czlx ==='';
      }
      return true;
    },
  },
  methods: {
    lookFlow() {
      this.lookFlowShow = true
    },
    //合并调用方法
    async mergeFn() {
      if(this.$store.state.flowInvoice.selectedItems.length  !==1){
        this.$message.error("请选择一条数据进行合并操作！")
        return false;
      }
      // 构造参数
      let param = {
        rdid: this.$store.state.flowInvoice.selectedItems[0].id,
        allMerge: false,
        //合并方式 0：按险种代码合并，1：按税率合并
        mergeMode: "0",
        //合并查询条件 ：0：业务编号，1：投保单号
        mergeQueryCondition: "0",
        pageNo: 1,
        pageSize: 10,
      }
      const {data, msg, code} = await mergeListQuery(param)
      if (code == 200){
        this.$refs.merge.show(data,this.$store.state.flowInvoice.selectedItems[0]);
        this.mergeShow = true
      }
    },
    splitFn() {
      this.splitShow = true
    },
    resList(val) {
      if(val){
        this.$store.dispatch('flowInvoice/getList')
      }

    },
    async reductionFn() {
      let czlx = this.$store.state.flowInvoice.selectedItems[0].czlx
      let rdid = this.$store.state.flowInvoice.selectedItems[0].id
      // 调用查验接口
      const {code, msg, data} = await checkFlowInvoiceStatus(rdid)
      if (code == 200) {
        this.$refs.flowMergingSplit.openDialog();
        this.$refs.flowMergingSplit.getList(rdid, czlx, "edit");
      }
    },
    openInvoice(isSpec) {
      if (isSpec === true) {
        this.invoicingSpectShow = true
        this.invoicingPlainShow = false
      } else {
        this.invoicingSpectShow = false
        this.invoicingPlainShow = true
      }
    },

    invoicingShow(val) {
      this.invoicingSpectShow = val.dialogShow
      this.invoicingPlainShow = val.dialogShow
      if (this.$store.state.flowInvoice.tableRef && val.refreshSelect) {
        this.$store.state.flowInvoice.tableRef.clearSelection()
      }
      //
      this.$store.dispatch('flowInvoice/getList')
    },
  },
  components: {
    addVoiceDialog,
    lookFlowDialog,
    invoicingDialog,
    splitDialog,
    mergeDialog,
    flowMergingSplitDialog
  }
}
</script>

<style lang="scss" scoped>
.btnDialog {
  margin-bottom: 6px;
}
</style>
